package com.lincoln.tb.spider;

import com.lincoln.tb.spider.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

@EnableDiscoveryClient
@SpringBootApplication
@Controller
public class SpiderApplication {
    private static Logger logger = LoggerFactory.getLogger(SpiderApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(SpiderApplication.class, args);
    }

    public static boolean complate = false;

    @PostMapping("/download")
    public void download(HttpServletResponse response, String name, String urls, Boolean onlyOne, @RequestParam(value = "file") MultipartFile file) throws Exception {
        complate = false;
        if (file != null) {
            InputStream inputStream = file.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder sb = new StringBuilder();

            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                String fileUrls = sb.toString();
                if (urls == null) {
                    urls = fileUrls;
                } else {
                    urls = urls + "\n" + fileUrls;
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    logger.error("{}", e);
                }
            }
        }
        String[] urlss = urls.split("\n");
        response.setContentType("application/x-msdownload;");
        response.setHeader("Content-disposition", "attachment; filename=" + new String(name.getBytes("utf-8"), "ISO8859-1"));
        byte[] bs = Utils.spiderPic(urlss, onlyOne == null ? false : onlyOne);
        response.getOutputStream().write(bs);
        response.getOutputStream().flush();
        response.getOutputStream().close();
        complate = true;
    }

    @ResponseBody
    @PostMapping("/downloadComplate")
    public String downloadComplate() throws InterruptedException {
        complate = false;
        Thread.sleep(1000);
        while (!complate) {
            Thread.sleep(100);
        }
        return "true";
    }

}
